package com.jltx.xq.simplewatch.main.adapter;

import android.content.Context;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.jltx.xq.simplewatch.R;
import com.jltx.xq.simplewatch.base.BaseSupportFragment;

import java.util.List;

import cn.jltx.xq.bottombarlayout.BottomBarItem;
import cn.jltx.xq.bottombarlayout.BottomBarLayout;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/19.
 */

public class MainFragmentPagerAdapter extends FragmentStatePagerAdapter implements BottomBarLayout.BottomBarItemProvider{
    private Context context;
    private List<MainTabFragmentInfo> fragmentInfoList;

    public MainFragmentPagerAdapter(FragmentManager fm, Context context, List<MainTabFragmentInfo> fragmentInfoList) {
        super(fm);
        if(context == null){
            throw new IllegalArgumentException("context is null");
        }
        this.context = context;
        this.fragmentInfoList = fragmentInfoList;
    }

    @Override
    public Fragment getItem(int position) {
        if(fragmentInfoList != null){
            return fragmentInfoList.get(position).fragment;
        }
        return null;
    }

    @Override
    public BottomBarItem getPageBottomBarItem(int position) {
        MainTabFragmentInfo mainTabFragmentInfo = fragmentInfoList.get(position);
        BottomBarItem bottomBarItem = new BottomBarItem(context);
        bottomBarItem.setIconDrawableRes(mainTabFragmentInfo.tabIconNormal,mainTabFragmentInfo.tabIconSelect)
                .setMaskColorRes(mainTabFragmentInfo.tabNormalColor,mainTabFragmentInfo.tabSelectColor)
                .setGradientText(mainTabFragmentInfo.tabTitle)
                .setGradientTextSizeRes(R.dimen.bottombar_text_size)
                .setGradientTextColorRes(mainTabFragmentInfo.tabNormalColor,mainTabFragmentInfo.tabSelectColor)
                .setShowBottomText(true);

        return bottomBarItem;
    }

    @Override
    public int getCount() {
        int count = 0;
        if(fragmentInfoList != null){
            count = fragmentInfoList.size();
        }
        return count;
    }

    public static class MainTabFragmentInfo{
        public BaseSupportFragment fragment;
        public @StringRes int tabTitle;
        public @DrawableRes int tabIconNormal;
        public @DrawableRes int tabIconSelect;
        public @ColorRes int tabNormalColor;
        public @ColorRes int tabSelectColor;

        public MainTabFragmentInfo(BaseSupportFragment fragment,  @StringRes int tabTitle, @DrawableRes int tabIconNormal, @DrawableRes int tabIconSelect, @ColorRes int tabNormalColor, @ColorRes int tabSelectColor) {
            this.fragment = fragment;
            this.tabTitle = tabTitle;
            this.tabIconNormal = tabIconNormal;
            this.tabIconSelect = tabIconSelect;
            this.tabNormalColor = tabNormalColor;
            this.tabSelectColor = tabSelectColor;
        }
    }
}
